	package com.hyts.threads.factory;

import com.hyts.threads.conf.ThreadPoolConfig;
import com.hyts.threads.handler.ThreadPoolExecuteHandler;

import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;


    /**
     * @author libo
     * @description 任务调度线程池工厂类
     * @date 2017-07-10
     */
    public class ScheduledThreadPoolFactory {


            /**
             * 创建底层任务调度线程池
             * @param corePoolSize 核心池大小
             * @return
             */
            public static ScheduledThreadPoolExecutor  createScheduleTask(int corePoolSize){
                corePoolSize = corePoolSize == 0? ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE:corePoolSize;
                return new ScheduledThreadPoolExecutor(corePoolSize);
            }


            /**
             * 创建底层任务调度线程池
             * @param corePoolSize  核心池大小
             * @param factory 创建线程的工厂方法类
             * @return
             */
            public static ScheduledThreadPoolExecutor  createScheduleTask(int corePoolSize,ThreadFactory factory){
                corePoolSize = corePoolSize == 0?ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE:corePoolSize;
                return new ScheduledThreadPoolExecutor(corePoolSize,factory == null ?Executors.defaultThreadFactory():factory);
            }

            /**
             * 创建底层任务调度线程池
             * @param corePoolSize
             * @param factory
             * @param handler
             * @return
             */
            public static ScheduledThreadPoolExecutor  createScheduleTask(int corePoolSize,ThreadFactory factory,RejectedExecutionHandler handler){
                corePoolSize = corePoolSize == 0?ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE:corePoolSize;
                handler = handler == null?new ThreadPoolExecuteHandler():handler;
                return new ScheduledThreadPoolExecutor(corePoolSize,factory == null ?Executors.defaultThreadFactory():factory,handler);
            }

    }
